<?php
class Administrator_fashion_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }		
	
	/*Content Management*/
	function content_management_add()
	{
		$is_publish = $this->input->post('is_publish');
		$fashion_publish_date = $this->input->post('fashion_publish_date');
		$is_main_hilight = $this->input->post('is_main_hilight');
		$is_type_hilight = $this->input->post('is_type_hilight');		
		$fashion_category = $this->input->post('fashion_category');
		$fashion_name = $this->input->post('fashion_name');
		$fashion_brief = $this->input->post('fashion_brief');
		$fashion_description = $this->input->post('fashion_description');
		$fashion_tag = $this->input->post('fashion_tag');
		$uploadify_store = explode(',',$this->input->post('uploadify_store'));
		$image_watermark_disable = $this->input->post('image_watermark_disable');
		$datetime = date('Y-m-d H:i:s');
		
		//Insert fashion
		$data = array(
			'fashion_category' => $fashion_category,
			'fashion_publish_date' => convertDatetime($fashion_publish_date),
			'fashion_name' => $fashion_name,
			'fashion_brief' => $fashion_brief,
			'fashion_description' => $fashion_description,
			'is_publish' => ''.$is_publish.'',
			'is_main_hilight' => ''.$is_main_hilight.'',
			'is_type_hilight' => ''.$is_type_hilight.'',			
			'fashion_tag' => ''.$fashion_tag.'',			
			'update_by' => $this->session->userdata('username'),
			'create_date' => $datetime,
			'update_date' => $datetime
		);
		$this->db->insert('fashion',$data);
		$fashion_id = $this->db->insert_id();
			
		//Insert fashion_image
		$dataImage = array();
		if(!empty($uploadify_store[0])) foreach($uploadify_store as $key => $value):
			//Resize Image
			$data = imageResize(array(
				'id' => $fashion_id,
				'source_image' => $this->temp_folder.$value,
				'new_path' => $this->upload_folder.'fashion/images/',
				'dimension' => array(												
					array(
						'width' => 120,
						'height' => 120
					)					
				),
				'watermark' => ($image_watermark_disable == false ? $this->upload_folder.'watermark/watermark.png':'')
			));			
			
			//delete temp
			@unlink($this->temp_folder.$value);
			
			$dataImage[] = array(
				'fashion_id' => $fashion_id,
				'fashion_file_name' => $data->file_name,
				'fashion_file_ext' => $data->file_ext
			);	
		endforeach;
		if(!empty($dataImage)) $this->db->insert_batch('fashion_image',$dataImage);
		
		//Delete Memcache
		$this->memcache->delete('dataFashionHilight');
		$this->memcache->delete('dataFashionUpdate');
		$this->memcache->delete('dataModuleFashionHot');
		$this->memcache->delete('dataHomeFashion');
		
		//Redirect to Edit Page
		redirect('fashion/content_management/edit/'.$fashion_id);		
	}
	
	function content_management_edit($fashion_id)
	{
		$is_publish = $this->input->post('is_publish');
		$fashion_publish_date = $this->input->post('fashion_publish_date');
		$is_main_hilight = $this->input->post('is_main_hilight');
		$is_type_hilight = $this->input->post('is_type_hilight');		
		$fashion_category = $this->input->post('fashion_category');
		$fashion_name = $this->input->post('fashion_name');
		$fashion_brief = $this->input->post('fashion_brief');
		$fashion_description = $this->input->post('fashion_description');
		$fashion_tag = $this->input->post('fashion_tag');
		$uploadify_store = explode(',',$this->input->post('uploadify_store'));
		$image_watermark_disable = $this->input->post('image_watermark_disable');
		$datetime = date('Y-m-d H:i:s');

		//Insert to fashion
		$data = array(
			'fashion_category' => $fashion_category,
			'fashion_publish_date' => convertDatetime($fashion_publish_date),
			'fashion_name' => $fashion_name,
			'fashion_brief' => $fashion_brief,
			'fashion_description' => $fashion_description,
			'is_publish' => ''.$is_publish.'',
			'is_main_hilight' => ''.$is_main_hilight.'',
			'is_type_hilight' => ''.$is_type_hilight.'',
			'fashion_tag' => ''.$fashion_tag.'',				
			'update_by' => $this->session->userdata('username'),
			'update_date' => $datetime
		);
		$this->db->update('fashion',$data,array('fashion_id' => $fashion_id));
		
		//Insert fashion_image
		$dataImage = array();
		if(!empty($uploadify_store[0])) foreach($uploadify_store as $key => $value):
			//Resize Image
			$data = imageResize(array(
				'id' => $fashion_id,
				'source_image' => $this->temp_folder.$value,
				'new_path' => $this->upload_folder.'fashion/images/',
				'dimension' => array(											
					array(
						'width' => 120,
						'height' => 120
					)				
				),
				'watermark' => ($image_watermark_disable == false ? $this->upload_folder.'watermark/watermark.png':'')
			));		
			
			//delete temp
			@unlink($this->temp_folder.$value);
			
			$dataImage[] = array(
				'fashion_id' => $fashion_id,
				'fashion_file_name' => $data->file_name,
				'fashion_file_ext' => $data->file_ext
			);	
		endforeach;
		if(!empty($dataImage)) $this->db->insert_batch('fashion_image',$dataImage);

		//Delete Memcache
		$this->memcache->delete('dataFashionHilight');
		$this->memcache->delete('dataFashionUpdate');
		$this->memcache->delete('dataModuleFashionHot');
		$this->memcache->delete('dataHomeFashion');				
	}	
	
	function ajax_update_image()
	{
		$fashion_image_id = $this->input->post('fashion_image_id');
		if(!empty($fashion_image_id)) foreach($fashion_image_id as $key => $value):
			$this->db->update('fashion_image',array('fashion_image_order' => $key),array('fashion_image_id' => $value));
		endforeach;
		exit;
	}
	
	function ajax_delete_image($fashion_image_id)
	{
		$dataImage = $this->db->get_where('fashion_image',array('fashion_image_id' => $fashion_image_id),1)->row();
		$this->db->delete('fashion_image',array('fashion_image_id' => $fashion_image_id));
		//Delete All Image
		$map = directory_map($this->upload_folder.'fashion/images/', 1);
		foreach($map as $folder):
			@unlink($this->upload_folder.'fashion/images/'.$folder.'/'.$dataImage->fashion_file_name.'.'.$dataImage->fashion_file_ext);
		endforeach;
		exit;
	}	
	
	function content_management_delete()
	{
		$id = $this->input->post('id');
		$map = directory_map($this->upload_folder.'fashion/images/', 1);
		foreach($id as $value):
			$dataImage = $this->db->get_where('fashion_image',array('fashion_id' => $value))->result();
			$tables = array('fashion','fashion_image','fashion_click_stat');
			$this->db->where('fashion_id', $value);
			$this->db->delete($tables);
			//Delete All Image
			foreach($map as $folder):
				foreach($dataImage as $items):
					@unlink($this->upload_folder.'fashion/images/'.$folder.'/'.$items->fashion_file_name.'.'.$items->fashion_file_ext);
				endforeach;
			endforeach;					
		endforeach;
		
		//Delete Memcache
		$this->memcache->delete('dataFashionHilight');
		$this->memcache->delete('dataFashionUpdate');
		$this->memcache->delete('dataModuleFashionHot');
		$this->memcache->delete('dataHomeFashion');				
	}
	/*End Content Management*/
}

?>